package io.gitee.zhangbinhub.acp.cloud.resource.server.component

import cn.dev33.satoken.stp.StpUtil
import io.gitee.zhangbinhub.acp.boot.constant.AcpCoreConstant
import io.gitee.zhangbinhub.acp.cloud.resource.server.tools.TokenTools
import org.noear.solon.core.handle.Context
import org.noear.solon.core.handle.Filter
import org.noear.solon.core.handle.FilterChain

class AcpCloudResourceServerGlobalFilter : Filter {
    override fun doFilter(ctx: Context, chain: FilterChain) {
        ctx.attrSet(AcpCoreConstant.HTTP_CLIENT_IP_KEY, ctx.realIp())
        TokenTools.getAccessTokenModel()?.also { accessToken ->
            StpUtil.getStpLogic().setTokenValueToStorage(
                StpUtil.getTokenValueByLoginId(
                    accessToken.getLoginId(),
                    accessToken.clientId
                )
            )
        }
        chain.doFilter(ctx)
    }
}